package org.ws.edu.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.ws.edu.common.model.Appointment;

public class AppointmentDao extends GenericDao<Appointment> {

    @Override
    public Appointment get(final Long id) {
        return hibernateTemplate.execute(new HibernateCallback<Appointment>() {

            public Appointment doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Appointment.class);
                criteria.add(Property.forName("id").eq(id));
                return (Appointment) criteria.uniqueResult();
            }
        });
    }

    @Override
    public void delete(final Long id) {
        hibernateTemplate.execute(new HibernateCallback<Boolean>() {

            public Boolean doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Appointment.class);
                criteria.add(Property.forName("id").eq(id));
                if (criteria.uniqueResult() != null) {
                    session.delete(criteria.uniqueResult());
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    @Override
    public List<Appointment> getList() {
        return hibernateTemplate.execute(new HibernateCallback<List<Appointment>>() {

            public List<Appointment> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Appointment.class);
                return criteria.list();
            }
        });
    }

}
